package com.polaris.live.widget.marqueen

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.ViewFlipper
import androidx.lifecycle.LifecycleCoroutineScope
import com.polaris.live.R
import com.polaris.live.resp.back_resp.HonorRankResp

/**
 * RankMarqueeView
 * @author Created by 天晴 on 2024/11/25 14:41
 * @since 1.0.0
 **/
class RankMarqueeView(context: Context, attrs: AttributeSet?) : ViewFlipper(context, attrs) {

    /**
     * Item翻页时间间隔【mInterval 必须大于 mAnimDuration，否则视图将会重叠】
     */
    private var mInterval = 3000

    /**
     * Item动画执行时间
     */
    private var mAnimDuration = 500

    //进出动画
    private var mAnimIn: Animation? = null
    private var mAnimOut: Animation? = null
    private var mAnimInRes: Int = R.anim.marquee_rank_bottom_in
    private var mAnimOutRes: Int = R.anim.marquee_rank_top_out
    private var inflater: LayoutInflater

    init {
        val a: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.RankMarqueeView, 0, 0)
        mInterval = a.getInt(R.styleable.RankMarqueeView_rmq_interval, mInterval)
        mAnimInRes = a.getResourceId(R.styleable.RankMarqueeView_rmq_animIn, mAnimInRes)
        mAnimOutRes = a.getResourceId(R.styleable.RankMarqueeView_rmq_animOut, mAnimOutRes)
        mAnimDuration = a.getInt(R.styleable.RankMarqueeView_rmq_animDuration, mAnimDuration)
        a.recycle()
        flipInterval = mInterval
        mAnimIn = AnimationUtils.loadAnimation(context, mAnimInRes)
        mAnimIn?.duration = mAnimDuration.toLong()
        inAnimation = mAnimIn
        mAnimOut = AnimationUtils.loadAnimation(context, mAnimOutRes)
        mAnimOut?.duration = mAnimDuration.toLong()
        outAnimation = mAnimOut
        inflater = LayoutInflater.from(context)
    }

    fun setActivitiesScroll(lifecycleScope: LifecycleCoroutineScope, honors: List<HonorRankResp>) {
        val honorMap = honors.associateBy { it.banner }
        val viewsToRemove = mutableListOf<View>()
        for (i in 0 until childCount) {
            val view = getChildAt(i)
            val tag = view.tag as? String
            if (tag != null && honorMap[tag] == null) {
                viewsToRemove.add(view)
            }
        }
        viewsToRemove.forEach { removeView(it) }
        honors.forEach { honor ->
            val view = findViewWithTag<RankBannerView>(honor.banner)
            if (view == null) {
                addView(RankBannerView(context, null).apply {
                    setData(lifecycleScope, honor)
                    tag = honor.banner
                })
            } else {
                view.setData(lifecycleScope, honor)
            }
        }
        if (childCount > 1 && !isFlipping) {
            postDelayed(back, mInterval.toLong())
        } else if (childCount <= 1 && isFlipping) {
            stopFlipping()
        }
    }

    private val back = {
        if (childCount > 1 && !isFlipping) {
            displayedChild = 1
            startFlipping()
        }
    }

    /**
     * 设置翻页时间间隔
     * @param interval
     */
    fun setInterval(interval: Int) {
        mInterval = interval
        flipInterval = interval
    }

    /**
     * 设置动画执行时间
     * @param animDuration
     */
    fun setAnimDuration(animDuration: Int) {
        mAnimDuration = animDuration
        mAnimIn?.duration = animDuration.toLong()
        inAnimation = mAnimIn
        mAnimOut?.duration = animDuration.toLong()
        outAnimation = mAnimOut
    }

    fun setAnimInAndOut(animIn: Animation?, animOut: Animation?) {
        mAnimIn = animIn
        mAnimOut = animOut
        inAnimation = animIn
        outAnimation = animOut
    }

    fun setAnimInAndOut(animInId: Int, animOutID: Int) {
        mAnimIn = AnimationUtils.loadAnimation(context, animInId)
        mAnimOut = AnimationUtils.loadAnimation(context, animOutID)
        mAnimIn?.duration = mAnimDuration.toLong()
        mAnimOut?.duration = mAnimDuration.toLong()
        inAnimation = mAnimIn
        outAnimation = mAnimOut
    }

    val animIn: Animation?
        get() = mAnimIn
    val animOut: Animation?
        get() = mAnimOut

    protected override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        stopFlipping()
        removeCallbacks(back)
    }
}